#pragma once

#include "parameter.h"

void movephoto(int lcd_fd)
{
    int count = 0;
    while (count < 4)
    {
        show_bmp(photo_name[count], lcd_fd, 0, 0);
        sleep(1);
        count++;
    }
}

void *photo_func(void *args)
{
    pthread_detach(pthread_self());
    char *msg = (char *)args;
    printf("%s\n", msg);
    //  打开LCD屏
    int lcd_fd = open("/dev/fb0", O_RDWR);
    if (lcd_fd == -1)
    {
        perror("open lcd failed!\n");
    }

    int lcd_fd_2 = open("/dev/fb0", O_RDWR);
    if (lcd_fd_2 == -1)
    {
        perror("open lcd failed!\n");
    }
    // 3.申请映射内存
    int *lcd_p = mmap(NULL, 800 * 480 * 4, PROT_READ | PROT_WRITE, MAP_SHARED, lcd_fd_2, 0);
    for (int y = 0; y < 480; y++)
    {
        for (int x = 0; x < 800; x++)
        {
            *(lcd_p + (800 * y + x)) = 0x00ffffff;
        }
    }

    // 打开触摸屏
    int ts_fd = open("/dev/input/event0", O_RDONLY);
    if (ts_fd == -1)
    {
        printf("打开触摸屏失败！\n");
    }
    struct input_event touch;
    int count = 0;    // 切换图片作用
    int x = 0, y = 0; // 触摸屏的xy
    int x_start = 0;
    int x_stop = 0;
    int x_start_1 = 0;
    int x_stop_1 = 0;
    // 偏移
    int excursion_x = 100;
    int excursion_y = 50;

    show_bt_bmp("return.bmp", lcd_fd, 0, 0);
    show_bt_bmp("left.bmp", lcd_fd, 0, 205);
    show_bt_bmp("right.bmp", lcd_fd, 729, 205);
    show_bmp(photo_name[count % 4], lcd_fd, excursion_x, excursion_y);

    while (1)
    {

        // 触摸选择
        read(ts_fd, &touch, sizeof(touch)); // 没触摸的时候。read是堵塞的
        if (touch.type == EV_ABS && touch.code == ABS_X)
            x = touch.value * 800 / 1024; // 判断是不是触摸屏设备，在判断是不是x坐标，
        if (touch.type == EV_ABS && touch.code == ABS_Y)
            y = touch.value * 480 / 600;
        // printf("x:%d--y:%d\n", x, y); // 黑板
        // usleep(5000);

        if (touch.type == EV_KEY && touch.code == BTN_TOUCH && touch.value != 0)
        {
            printf("FINGTER DOWN(%d ,%d )\n", x, y);
            x_start = x;
        }
        // 弹起来
        if (touch.type == EV_KEY && touch.code == BTN_TOUCH && touch.value == 0)
        {
            printf("FINGTER UP(%d ,%d )\n", x, y);
            x_stop = x;
        }
        if (touch.type == EV_KEY && touch.code == BTN_TOUCH && touch.value != 0)
        {
            printf("FINGTER DOWN(%d ,%d )\n", x, y);
            x_start_1 = x;
        }
        // 弹起来
        if (touch.type == EV_KEY && touch.code == BTN_TOUCH && touch.value == 0)
        {
            printf("FINGTER UP(%d ,%d )\n", x, y);
            x_stop_1 = x;
        }
        if (y >= 400 && (x_stop - x_start) >= 300)
        {
            count++;
            if (count > 3)
                count = 0;
            show_bmp(photo_name[count % 4], lcd_fd, excursion_x, excursion_y);
            x_start = x_stop = 0;
            usleep(5000);
        }

        if (y >= 400 && x_start_1 - x_stop_1 > 300)
        {
            count--;
            if (count < 0)
                count = 3;
            show_bmp(photo_name[count % 4], lcd_fd, excursion_x, excursion_y);
            x_start_1 = x_stop_1 = 400;
            usleep(5000);
        }

        if (touch.type == EV_KEY && touch.code == BTN_TOUCH && touch.value == 0) // 判断是否手离开
        {
            for (int y = 0; y < 480; y++)
            {
                for (int x = 0; x < 800; x++)
                {
                    *(lcd_p + (800 * y + x)) = 0x00ffffff;
                }
            }
            show_bt_bmp("left.bmp", lcd_fd, 0, 205);
            show_bt_bmp("right.bmp", lcd_fd, 729, 205);
            show_bmp(photo_name[count % 4], lcd_fd, excursion_x, excursion_y);

            printf("x:%d--y:%d\n", x, y);                  // 黑板
            if (x >= 0 && x <= 70 && y >= 205 && y <= 275) // 往回
            {
                count--;
                if (count < 0)
                    count = 3;
                show_bmp(photo_name[count % 4], lcd_fd, excursion_x, excursion_y);
            }
            else if (x >= 729 && x < 800 && y >= 205 && y <= 275) // 往前
            {
                count++;
                if (count > 3)
                    count = 0;
                show_bmp(photo_name[count % 4], lcd_fd, excursion_x, excursion_y);
            }
            // 返回页面功能
            show_bt_bmp("return.bmp", lcd_fd, 0, 0);
            if ((x >= 0 && x <= 70) && (y >= 0 && y <= 70))
            {
                flag = MENU;
                close(lcd_fd);
                close(ts_fd);
                munmap(lcd_p, 800 * 480 * 4);
                break;
            }
        }
        if (x >= 100 && x <= 700 && y >= 50 && y <= 400) // 实现拖动
        {
            // 刷屏
            for (int y = 0; y < 480; y++)
            {
                for (int x = 0; x < 800; x++)
                {
                    *(lcd_p + (800 * y + x)) = 0x00000000;
                }
            }
            show_bmp(photo_name[count % 4], lcd_fd_2, x - 400, y - 240);
        }
    }
}